Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

176
Visualizações
In Chrome Extension, Javascript's ".click()" method is supposedly synchronous, but the website doesn't update until after code is finished

I have an extension for Google Chrome where I attempt to trigger a click of a button on the page, then wait 2 seconds, then print a message. When the button is clicked, it turns blue to show that it has been selected.

let element = document.getElementsByClassName("Button Button--alt Button--custom move-action-btn roster-action-btn").item(0);
element.addEventListener(("click"), function(){
    console.log("clicked")
})
element.click()
sleep(2000)
console.log("done")

I would expect this to click the button (and I see it turn blue), log "clicked", wait 2 seconds, then log "done". However, it logs "clicked", waits 2 seconds, logs "done", then the button turns blue and appears to have been clicked, seemingly immidiately after it logs "done".

If Javascript .click() is synchronous, why does the button appear to be clicked after the function is ran, and why is the actual result of the click after the event listener?

The sleep function for those wondering:

function sleep(milliseconds) {
    const date = Date.now();
    let currentDate = null;
    do {
      currentDate = Date.now();
    } while (currentDate - date < milliseconds);
}
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda